﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LambdaExtensions.WinForms;
using LambdaExtensions.TreeExtensions;
using System.Threading;

namespace ExampleApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Random r = new Random();

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                BackgroundOperation.ForEach(Enumerable.Range(0, 100), i => Thread.Sleep(500));

            }
            catch (ThreadInterruptedException)
            {

                MessageBox.Show("Canceled");
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            treeView1.ExpandAll();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            var c = Color.FromArgb(r.Next());

        //    foreach (var n in treeView1.Nodes.OfType<TreeNode>().SelectTreeManyDeep(n => n.Nodes.OfType<TreeNode>()))
       //         n.BackColor = c;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            listBox1.DataSource = treeView1.Nodes.OfType<TreeNode>().SelectTreeManyWide(n => n.Nodes.OfType<TreeNode>()).ToList();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            listBox1.DataSource = treeView1.Nodes.OfType<TreeNode>().SelectTreeManyDeep(n => n.Nodes.OfType<TreeNode>()).ToList();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            treeView1.Nodes.OfType<TreeNode>().SelectTreeManyWide(n => n.Nodes.OfType<TreeNode>()).ToList().ForEach(n => n.BackColor = Color.White);

            var c = Color.FromArgb(r.Next());

           (listBox1.SelectedItem as TreeNode).BackColor = c;
        }
    }
}
